﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Login
/// </summary>

namespace LoginDetails
{
    public class CheckLogin
    {
        public CheckLogin()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private static string connectionString = ConfigurationManager.ConnectionStrings["connectUserManager"].ToString();
        private static SqlConnection _sqlcon = new SqlConnection(connectionString);
       
        public static SqlConnection Sqlcon
        {
            get { return _sqlcon; }
            set { _sqlcon = value; }
        }

       SqlDataAdapter _sqlada = new SqlDataAdapter("", Sqlcon);
        public  SqlDataAdapter SqlAda
        {
            get
            {
                return _sqlada;
            }
            set
            {
                _sqlada = value;
            }
        }

       

       
       /// <summary>
       /// 
       /// </summary>
       /// <param name="userName"></param>
       /// <param name="password"></param>
       /// <param name="dsLogin"></param>
       /// <remarks>
        /// This function checks the database table with the given username and password and if there is match ,ie
        /// if such user exists,it will return true.Otherwise false is returned.The dataset dsLogin will contain 
        /// the details of user such as name,type etc..         
       /// </remarks>
       /// <returns></returns>
        public bool ValidateLogin(string userName,string password,out DataSet dsLogin)
        {
            dsLogin = new DataSet();
            SqlAda.SelectCommand.CommandText = "SELECT UD.UD_UserPersonalID,UD.UD_FirstName,US.US_GroupID,US.US_GroupName " +
            "from UserList as UL,UserDetails as UD,UserGroup as US,UserGroupMap as UR  where  UL.UL_UserPersonalID " +
            "= UD.UD_UserPersonalID and UD.UD_UserPersonalID = UR.UR_UserID and  UR.UR_UserGroupID = US.US_GroupID " +
            "and UL_UserName =  '" + userName + "' and UL_Password = '" + password  + "'";

            SqlAda.Fill(dsLogin);
            if (dsLogin.Tables[0].Rows.Count > 0)
                return true;
            else
                return false;
            
        }

        public string GetPassword(string userName,string authenticationQuest,string answer)
        {
            #region NameSpace
            int recCount = 0;
            string password = string.Empty;
            EB.Generics.Generics commonClass = new EB.Generics.Generics();
            string query = string.Empty;
            #endregion

            query ="SELECT      UserList.UL_Password FROM  UserList INNER JOIN UserDetails ON "+
                  " UserList.UL_UserPersonalID = UserDetails.UD_UserPersonalID where UserList.UL_UserName='" + userName + "' and " +
                  " UserList.UL_AuthQuestion='" + authenticationQuest + "' and UserList.UL_AuthAnswer='" + answer + "'";
            DataSet uderDetailsDS = commonClass.ExecuteQuery(query);
            if (uderDetailsDS.Tables[0].Rows.Count > 0)
            {
                password = uderDetailsDS.Tables[0].Rows[0][0].ToString();
            }
            return password;
        }

    }
}
